<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

/**
 * 统计管理
 * @author LIYANG
 *
 */
class manage extends admin {
	private $db;
	private $admin_db;
	public $siteid;
	
	public function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('news_nsr_model');//加载资讯模型
		$this->admin_db = pc_base::load_model('admin_model');//加载管理员模型
		$this->siteid = get_siteid();//获取站点
	}
	
	public function init() {
		$category_arr = array(
				'1'=>'资讯',
				'2'=>'视频',
				'3'=>'图集'
		);
		$page = $_GET['page']?(int)$_GET['page']:1;
		$pagesize= $_GET['num']?(int)$_GET['num']:10;
		$search_info = $_GET['account'];
		$where = "";
		$model = "";
		if ($search_info) {
			if (!empty($search_info['begin_time'])) {
				$begin_time = $search_info['begin_time'];
				$where = " where create_time > '$begin_time'";
			}
			if (!empty($search_info['end_time'])) {
				$end_time = $search_info['end_time'];
				$where = "where create_time < '$end_time'";
			}
			if (!empty($search_info['begin_time']) && !empty($search_info['end_time'])) {
				$begin_time = $search_info['begin_time'];
				$end_time = $search_info['end_time'];
				$where = "where create_time > '$begin_time' and create_time < '$end_time'";
			}
			if (!empty($search_info['resource'])) {
				switch ($search_info['resource']){
					case 1:
						$model = 'nsrweb_news_nsr ';
						break;
					case 2:
						$model = 'nsrweb_video_nsr ';
						break;
					case 3:
						$model = 'nsrweb_picture_nsr ';
						break;
				}
				$sql= "select userid,realname,nums from (SELECT created_by,count(created_by) as nums FROM ".$model.$where."
 					GROUP BY created_by ORDER BY nums desc) as news left join nsrweb_admin as admin on news.created_by = admin.userid";
				$resource = $this->db->query($sql);
				$account_info= $this->db->fetch_array();
			} else {
				$sql_news = "select userid,realname,nums from (SELECT created_by,count(created_by) as nums FROM nsrweb_news_nsr ".$where."
 					GROUP BY created_by ORDER BY nums desc) as news left join nsrweb_admin as admin on news.created_by = admin.userid";
				$resource_news = $this->db->query($sql_news);
				$news_info = $this->db->fetch_array();
				
				$sql_video = "select userid,realname,nums from (SELECT created_by,count(created_by) as nums FROM nsrweb_video_nsr ".$where."
 					GROUP BY created_by ORDER BY nums desc) as news left join nsrweb_admin as admin on news.created_by = admin.userid";
				$resource_video = $this->db->query($sql_video);
				$video_info = $this->db->fetch_array();
				
				$sql_picture= "select userid,realname,nums from (SELECT created_by,count(created_by) as nums FROM nsrweb_picture_nsr ".$where."
 					GROUP BY created_by ORDER BY nums desc) as news left join nsrweb_admin as admin on news.created_by = admin.userid";
				$resource_picture= $this->db->query($sql_picture);
				$picture_info = $this->db->fetch_array();
				if(!empty($news_info) || !empty($video_info) || !empty($picture_info)){
					foreach ($news_info as $k=>$v){
						if ($v['userid']!="") {
							$news_info_arr[$v['userid']] = $v['nums'];
						}
					}
					foreach ($video_info as $kv=>$vv){
						if ($vv['userid'] != "") {
							$video_info_arr[$vv['userid']] = $vv['nums'];
						}
					}
					foreach ($picture_info as $ks=>$vs){
						if ($vs['userid'] !="") {
							$picture_info_arr[$vs['userid']] = $vs['nums'];
						}
					}
					$newArr = $this->array_value_sum($news_info_arr, $video_info_arr, $picture_info_arr);
					$admin_sql = "select userid,realname from nsrweb_admin where userid in (";
					foreach ($newArr as $k=>$v){
						$admin_sql .= "$k,";
					}
					$admin_sql = trim($admin_sql,',').")";
					$resource = $this->db->query($admin_sql);
					$admin_info = $this->db->fetch_array();
					$account_info = array();
					foreach ($admin_info as $ka=>$va){
						foreach ($newArr as $key=>$value){
							if($va['userid'] == $key){
								$account_info[$ka] = $va;
								$account_info[$ka]['nums'] = $value;
							}
						}
					}
					array_multisort(array_column($account_info,'nums'),SORT_DESC,$account_info);
				} else {
					$account_info = "";
				}
				
			}
			
		} else {
			$sql_news= "select userid,realname,nums from (SELECT created_by,count(created_by) as nums FROM nsrweb_news_nsr ".$where."
 					GROUP BY created_by ORDER BY nums desc) as news left join nsrweb_admin as admin on news.created_by = admin.userid";
			$resource_news = $this->db->query($sql_news);
			$news_info= $this->db->fetch_array();//获取资讯录入信息
			
			$sql_video = "select userid,realname,nums from (SELECT created_by,count(created_by) as nums FROM nsrweb_video_nsr ".$where."
 					GROUP BY created_by ORDER BY nums desc) as news left join nsrweb_admin as admin on news.created_by = admin.userid";
			$resource_video = $this->db->query($sql_video);
			$video_info = $this->db->fetch_array();//获取视频录入信息
			
			$sql_picture = "select userid,realname,nums from (SELECT created_by,count(created_by) as nums FROM nsrweb_picture_nsr ".$where."
 					GROUP BY created_by ORDER BY nums desc) as news left join nsrweb_admin as admin on news.created_by = admin.userid";
			$resource_picture = $this->db->query($sql_picture);
			$picture_info = $this->db->fetch_array();//获取图集录入信息
			
			if(!empty($news_info) || !empty($video_info) || !empty($picture_info)){
				foreach ($news_info as $k=>$v){
					if ($v['userid']!="") {
						$news_info_arr[$v['userid']] = $v['nums'];
					}
				}
				foreach ($video_info as $kv=>$vv){
					if ($vv['userid'] != "") {
						$video_info_arr[$vv['userid']] = $vv['nums'];
					}
				}
				foreach ($picture_info as $ks=>$vs){
					if ($vs['userid'] !="") {
						$picture_info_arr[$vs['userid']] = $vs['nums'];
					}
				}
				$newArr = $this->array_value_sum($news_info_arr, $video_info_arr, $picture_info_arr);//合并资讯、视频和图集的录入信息，将userid相同的数量值累加
				$admin_sql = "select userid,realname from nsrweb_admin where userid in (";
				foreach ($newArr as $k=>$v){
					$admin_sql .= "$k,";
				}
				$admin_sql = trim($admin_sql,',').")";
				$resource = $this->db->query($admin_sql);//查询编辑者信息
				$admin_info = $this->db->fetch_array();
				$account_info= array();
				foreach ($admin_info as $ka=>$va){
					foreach ($newArr as $key=>$value){
						if($va['userid'] == $key){
							$account_info[$ka] = $va;
							$account_info[$ka]['nums'] = $value;
						}
					}
				}//组合编辑者信息和录入资源的数量为新数组
				array_multisort(array_column($account_info,'nums'),SORT_DESC,$account_info);//根据录入数量降序排序
			} else {
				$account_info = "";
			}
		}
		pc_base::load_sys_class('form','',0);
		include $this->admin_tpl('account_list');
	}
	
	/**
	* 数组根据相同的key值将value值累加
	* @date: 2017年8月24日 上午10:22:24
	* @author: LIYANG
	*/
	public function array_value_sum()
	{
		$res = array();
		foreach (func_get_args() as $arr) {
			foreach ($arr as $k => $v){
				if (!isset($res[$k])){
					$res[$k] = $v;
				}else{
					$res[$k] += $v;
				}
			}
		}
		return $res;
	}
	
}